package com.aurora.oa.work.view;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.aurora.oa.Constants.AuroraConstants;
import com.aurora.oa.R;
import com.aurora.oa.activity.BaseActivity;
import com.aurora.oa.bean.HttpResult;
import com.aurora.oa.listener.OnRequestHttpDataListener;
import com.aurora.oa.model.RequestHttpModel;
import com.aurora.oa.model.RequestHttpModelImpl;
import com.aurora.oa.utils.DateUtil;
import com.aurora.oa.utils.ToastUtil;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.qqtheme.framework.picker.DateTimePicker;
import cn.qqtheme.framework.picker.OptionPicker;

/**
 * Created by Administrator on 2017/5/2 0002.
 */
public class ApplyCarDetailActivity extends BaseActivity {
    private static final int START_TIME = 1;
    private static final int BACK_TIME = 2;
    ProgressDialog progressDialog;
    RequestHttpModel model = new RequestHttpModelImpl();
    DateTimePicker datePicker;
    @Bind(R.id.et_phone_number)
    EditText etPhoneNumber;
    @Bind(R.id.et_apply_reson)
    EditText etApplyReson;
    @Bind(R.id.tv_apply_time)
    TextView tvApplyTime;
    @Bind(R.id.tv_back_car_time)
    TextView tvBackCarTime;
    @Bind(R.id.tv_submit)
    TextView tvSubmit;
    @Bind(R.id.tv_history)
    TextView tvHistory;
    //选择的是哪个时间
    int selectDateType;
    String carID;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initTitlebar(true, "申请用车", true);
        setMyContentView(R.layout.activity_apply_car_detail);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        carID = getIntent().getStringExtra("id");
        datePicker = new DateTimePicker(this,DateTimePicker.HOUR_24);
        datePicker.setLabel("年","月","日","时","分");
        datePicker.setOnDateTimePickListener(new DateTimePicker.OnYearMonthDayTimePickListener() {
            @Override
            public void onDateTimePicked(String year, String month, String day, String hour, String minute) {
                switch (selectDateType){
                    case START_TIME:
                        tvApplyTime.setText(year+"年"+month+"月"+day+"日"+" "+hour+":"+minute);
                        break;
                    case BACK_TIME:
                        tvBackCarTime.setText(year+"年"+month+"月"+day+"日"+" "+hour+":"+minute);
                        break;
                }
            }
        });

    }
    public void doSubmit() {
        if(tvApplyTime.getText().toString().trim().equals("")){
            ToastUtil.show(this,"请选择申请时间");
            return;
        }
        if(tvBackCarTime.getText().toString().trim().equals("")){
            ToastUtil.show(this,"请选择还车时间");
            return;
        }

        if(etApplyReson.getText().toString().trim().equals("")){
            ToastUtil.show(this,"请输入申请原因");
            return;
        }
        if(etPhoneNumber.getText().toString().trim().equals("")){
            ToastUtil.show(this,"请输入联系电话");
            return;
        }

        String starttime = DateUtil.getDateString(tvApplyTime.getText().toString());
        String pre_backtime = DateUtil.getDateString(tvBackCarTime.getText().toString());
        String reason = etApplyReson.getText().toString().trim();
        String phone = etPhoneNumber.getText().toString().trim();
        String carid = carID;

        progressDialog = ProgressDialog.show(this, null, "正在提交数据...");
        Map<String, String> params = new HashMap<>();
        params.put("starttime", starttime==null?"":starttime);
        params.put("pre_backtime", pre_backtime==null?"":pre_backtime);
        params.put("reason", reason==null?"":reason);
        params.put("phone", phone==null?"":phone);
        params.put("carid", carid==null?"":carid);
        model.postForm(AuroraConstants.SEND_CAR_APPLY, params, new OnRequestHttpDataListener() {
            @Override
            public void onRequestDataSuccess(String body) {

                Gson gson = new Gson();
                HttpResult result = gson.fromJson(body, HttpResult.class);
                progressDialog.dismiss();
                if (result != null && result.success) {
                    ToastUtil.show(getApplicationContext(), "提交成功!");
                    finish();
                } else {
                    ToastUtil.show(getApplicationContext(), result.msg);
                }
            }

            @Override
            public void onRequestDataFail(String message) {
                ToastUtil.show(getApplicationContext(), message);
                progressDialog.dismiss();
            }
        });
    }
    @OnClick({R.id.tv_submit, R.id.tv_history,R.id.tv_apply_time,R.id.tv_back_car_time})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_submit:
                doSubmit();
                break;
            case R.id.tv_history:
                startActivity(new Intent(ApplyCarDetailActivity.this,ApplyCarHistoryListActivity.class));
                break;
            case R.id.tv_apply_time:
                selectDateType = START_TIME;
                datePicker.show();
                break;
            case R.id.tv_back_car_time:
                selectDateType = BACK_TIME;
                datePicker.show();
                break;

        }
    }
}
